/*
 * Group 3, User Interface Programming II (vt11)
 * Pooya | Flora | Monica | Can
 */
package vical.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import vical.model.DayModel;
import vical.model.Entry;
import vical.view.model.TimeLineModel;

/**
 * Customized JComponent to display entries (not include all-day entries)
 * of a single day, used by WeekView and DayView.
 * 
 * @author Can
 */
public class DayBoardContent extends JComponent {

    private DayModel dayModel;
    private TimeLineModel timeLineModel;
    private ArrayList<Component> components;

    public DayBoardContent(DayModel model, TimeLineModel timeLineModel) {
        this.dayModel = model;
        this.timeLineModel = timeLineModel;
        this.components = new ArrayList<Component>();
        initComponents();
    }

    private void initComponents() {
        //
        //this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        List<Entry> sortedEntries = this.getSortedEntries(dayModel.getAllDayEntries(false));
        this.createComponents(sortedEntries);
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        for (Component component : this.components) {
            this.add(component);
        }
    }

    // not finished
    private void createComponents(List<Entry> sortedEntries) {
        JPanel panel = null;
        int lastEnd = 0;
        for (Entry entry : sortedEntries) {
            int start = timeLineModel.getTimePositionY(entry.getStartTime());
            int end = timeLineModel.getTimePositionY(entry.getEndTime());
            if (components.isEmpty()) {
                components.add(Box.createRigidArea(new Dimension(this.getWidth(), start - 1)));
            }
            EntryCard rect = new EntryCard(entry);
            rect.setSize(this.getWidth(), end - start);
            if (start >= lastEnd) {
                if (panel != null) {
                    components.add(panel);
                }
                panel = new JPanel();
                panel.setOpaque(false);
                panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
            }
            panel.add(rect);
            lastEnd = end;
        }
        int spare = timeLineModel.getTimePositionY(timeLineModel.getHours()) - lastEnd;
        components.add(Box.createRigidArea(new Dimension(this.getWidth(), spare)));
    }

    // not finished
    private List<Entry> getSortedEntries(List<Entry> entries) {
        List<Entry> sortedEntries = new ArrayList<Entry>();
        for (Entry entry : entries) {
            sortedEntries.add(entry);
        }
        for (int i = 0; i < entries.size(); i++) {
            for (int j = 0; j < entries.size() - 1; j++) {
                if (sortedEntries.get(j).getStartTime().after(sortedEntries.get(j + 1).getStartTime())) {
                    Entry temp = sortedEntries.get(j);
                    sortedEntries.set(j, sortedEntries.get(j + 1));
                    sortedEntries.set(j + 1, temp);
                }
            }
        }
        return sortedEntries;
    }

    @Override
    protected void paintComponent(Graphics g) {
        Rectangle drawHere = g.getClipBounds();

        // Fill clipping area with dirty brown/orange.
        g.setColor(Color.WHITE);
        g.fillRect(drawHere.x, drawHere.y, drawHere.width, drawHere.height);

        // Font and Color
        g.setFont(new Font("SansSerif", Font.PLAIN, 10));
        g.setColor(Color.black);

        for (Entry entry : dayModel.getAllDayEntries(false)) {
        }

        super.paintComponent(g);
    }
}
